package cn.exrick.xboot.shop.controller;

import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.shop.entity.ShopArticleCollect;
import cn.exrick.xboot.shop.service.IShopArticleCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
* <p>
    * 用户收藏  前端控制器
    * </p>
*
* @author yuhaifei
* @since 2022-11-23
*/
@RestController
@RequestMapping("/xboot/shoparticlecollect")
public class ShopArticleCollectController {

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private IShopArticleCollectService collectService;

    /**
     * 收藏文章
     * @param collect
     * @return
     */
    @PostMapping("addCollect")
    public Result addCollect(ShopArticleCollect collect){
        collect.setUserId(securityUtil.getCurrMember().getId());
        collect.setCreateTime(new Date());
        collect.setUpdateTime(new Date());
        collectService.save(collect);
        return ResultUtil.data("收藏成功");
    }

    /**
     * 取消收藏
     * @param tid
     * @return
     */
    @PostMapping("removeCollect")
    public Result removeCollect(Integer tid){
        collectService.remove(new QueryWrapper<ShopArticleCollect>()
                .eq("user_id", securityUtil.getCurrMember().getId())
                .eq("tid", tid));
        return ResultUtil.data("取消成功");
    }

}
